package cn.edu.zju.oms.container;

import com.github.kfcfans.powerjob.worker.core.processor.ProcessResult;
import com.github.kfcfans.powerjob.worker.core.processor.TaskContext;
import com.github.kfcfans.powerjob.worker.core.processor.sdk.BasicProcessor;
import com.github.kfcfans.powerjob.worker.log.OmsLogger;
import org.apache.commons.net.ntp.NTPUDPClient;
import org.apache.commons.net.ntp.TimeInfo;
import org.apache.commons.net.ntp.TimeStamp;
import org.springframework.stereotype.Component;

import java.net.InetAddress;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * ContainerStandaloneProcessor
 *
 * @author tjq
 * @since 2020/5/17
 */
@Component
public class ContainerStandaloneProcessor implements BasicProcessor {

    @Override
    public ProcessResult process(TaskContext taskContext) throws Exception {

        OmsLogger log = taskContext.getOmsLogger();
        System.out.println(taskContext.getJobParams());
        System.out.println(taskContext.getInstanceParams());
        log.info("ContainerStandaloneProcessor#process");
        // 测试第三方库

        NTPUDPClient timeClient = new NTPUDPClient();
        timeClient.setDefaultTimeout(10000);
        String timeServerUrl = "ntp.sjtu.edu.cn";
        InetAddress timeServerAddress = InetAddress.getByName(timeServerUrl);
        TimeInfo timeInfo = timeClient.getTime(timeServerAddress);
        TimeStamp timeStamp = timeInfo.getMessage().getTransmitTimeStamp();
        Date date = timeStamp.getDate();
        log.info("ntp date from ntp server is: {}", date);
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

        log.info("current time is: {}", dateFormat.format(date));

        return new ProcessResult(true, "process success!");
    }

}
